package cn.fourhundredandeighteen.service.impl;

import cn.fourhundredandeighteen.dao.CourseDao;
import cn.fourhundredandeighteen.dao.ScoreDao;
import cn.fourhundredandeighteen.dao.TeacherDao;
import cn.fourhundredandeighteen.entity.Course;
import cn.fourhundredandeighteen.entity.Score;
import cn.fourhundredandeighteen.entity.Teacher;
import cn.fourhundredandeighteen.service.TeacherService;
import cn.fourhundredandeighteen.utill.MysqlConnect;
import cn.fourhundredandeighteen.view.vo.LoginVO;

import java.util.List;

/**
 * @description:
 * @author: liuyongqi
 * @time: 2020/12/15
 */
public class TeacherServiceImpl implements TeacherService {
    public String login(LoginVO loginVO) {
        TeacherDao teacherDao= MysqlConnect.into().getMapper(TeacherDao.class);
        try {
            String login = teacherDao.getLogin(loginVO);
            MysqlConnect.destroy();
            return login;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Teacher getTeacherInfo(Long teacherId) {
        TeacherDao teacherDao=MysqlConnect.into().getMapper(TeacherDao.class);
        try {
            Teacher teacher = teacherDao.selectByPrimaryKey(teacherId);
            MysqlConnect.destroy();
            return teacher;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Course> selectTeacherCourse(Long teacherId) {
        CourseDao courseDao=MysqlConnect.into().getMapper(CourseDao.class);
        try {
            List<Course> courses = courseDao.selectStudentCreateCourse(teacherId);
            MysqlConnect.destroy();
            return courses;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int createTeacher(Teacher teacher) {
        TeacherDao teacherDao=MysqlConnect.into().getMapper(TeacherDao.class);
        try {
            int i = teacherDao.insertSelective(teacher);
            MysqlConnect.destroy();
            if (i>0)
                return 1;
            else
                return 0;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public int upTeacher(Teacher teacher) {
        TeacherDao teacherDao=MysqlConnect.into().getMapper(TeacherDao.class);
        int i = teacherDao.updateByPrimaryKeySelective(teacher);
        MysqlConnect.destroy();
        if (i>0)
            return i;
        else
            return 0;
    }

    @Override
    public int deleteTeacher(Long teacherId) {
        TeacherDao teacherDao=MysqlConnect.into().getMapper(TeacherDao.class);
        int i = teacherDao.deleteByPrimaryKey(teacherId);
        MysqlConnect.destroy();
        if (i>0)
            return i;
        else
            return 0;
    }

    @Override
    public int delStudentScore(Long courseId, Long studentId) {
        ScoreDao scoreDao=MysqlConnect.into().getMapper(ScoreDao.class);
        Score score = scoreDao.selectByStudentScore(studentId, courseId);
        if (score==null){
            return 0;
        }
        int i = scoreDao.deleteByPrimaryKey(score.getScoresId());
        MysqlConnect.destroy();
        if (i>0){
            return i;
        }else{
            return 0;
        }
    }

    @Override
    public List<Teacher> selectTeacherAll() {
        TeacherDao teacherDao=MysqlConnect.into().getMapper(TeacherDao.class);
        List<Teacher> teachers = teacherDao.selectByTeacherAll();
        MysqlConnect.destroy();
        if (teachers!=null){
            return teachers;
        }
        return null;
    }
}
